package com.surfilter.mq.job.start;

import com.surfilter.mq.entity.WebsiteInfo;
import com.surfilter.mq.rocketmq.ProducerManage;
import com.surfilter.mq.util.ByteUtils;
import com.surfilter.mq.util.properties.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ValidateLiveSendTaskJob {

	private static final Logger logger = LoggerFactory
			.getLogger(ValidateLiveSendTaskJob.class);

	private boolean enable;
	/**
	 * @Title: work
	 * @Description: 
	 * @throws
	 */
	public void work() {
		if (!this.isEnable()) {
			return;
		}
		logger.info("测试-----开始下发网站验活任务");
		WebsiteInfo website = new WebsiteInfo();
		
		List <WebsiteInfo>list = this.getStatDaoImpl().getSqlSessionTemplate().selectList("getTestWebsiteInfo", website);
		
		if(list == null || list.isEmpty())return;
		
		logger.info("----------------下发网站验活任务数量：" + list.size());
		
		String producerName = PropertiesUtils.getStringValue("testwebsite.validate.producer.name","testws_vali_producer");
		String groupName = PropertiesUtils.getStringValue("testwebsite.validate.group","testws_vali_group");
		String nameServerAddreses = PropertiesUtils.getStringValue("rocketmq.namesrv");
		String topic = PropertiesUtils.getStringValue("testwebsite.validate.topic","testws_vali");
		
		byte[] bytes;
		try {
			ProducerManage.createProducerQueueNums(topic, producerName, groupName, nameServerAddreses, 16);
			for(int i =0;i < list.size();i++){
				bytes = ByteUtils.serialize(list.get(i));
				ProducerManage.sendMessage(producerName, topic, bytes);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("测试-----网站验活异常", e);
		}
		logger.info("测试-----下发网站验活任务完成");
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}
}
